package com.egoo.ticket.server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.dao.WorkOrderTypeNodeDao;
import com.egoo.ticket.server.pojo.entity.EwsUser;
import com.egoo.ticket.server.pojo.vo.WorkOrderTypeNodeVo;
import com.egoo.ticket.server.service.UserService;
import com.egoo.ticket.server.utils.common.CommonUtil;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Slf4j
@RestController
@RequestMapping(value = "/v1/user", produces = "application/json")
@Api(value = "用户管理",tags = "用户管理",description = "用户管理")
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private CommonUtil commonUtil;
	@Autowired
	private WorkOrderTypeNodeDao workOrderTypeNodeDao;

	@ApiOperation(value = "获取所有的用户列表", notes = "获取所有的用户列表")
	@RequestMapping(method = RequestMethod.GET, value = "/selectAll")
	public DbResponse selectAll(HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		Map<String, Object> searchMap = new HashMap<>(2);
		searchMap.put("tenantId",tenantId);
		List<EwsUser> userList = userService.getAllUserList(searchMap);
		dbResponse.setRetCode(200);
		dbResponse.setData(userList);
		return dbResponse;
	}

	@ApiOperation(value = "根据用户id获取用户信息", notes = "根据用户id获取用户信息")
	@RequestMapping(method = RequestMethod.GET, value = "/selectUserInfoById")
	public DbResponse selectUserInfoById(@ApiParam(value = "用户id", required = true) @RequestParam("id") String id,
										 HttpServletRequest request) {
		id = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		try {
			if (StringUtils.isEmpty(id)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "用户id不能为空！");
			}
			EwsUser ewsUserVo = userService.selectById(id);
			if (ewsUserVo==null) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, id+ "用户id不能为空！");
			}
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, ResponseConstant.SELECT_SUCCESS_MSG, ewsUserVo);
		} catch (Exception e) {
			log.info("获取用户信息异常信息：" + e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "获取用户信息异常信息：" + e);
		}
	}
	@ApiOperation(value = "根据部门id获取用户信息", notes = "根据部门id获取用户信息")
	@RequestMapping(method = RequestMethod.POST, value = "/getUseListByDepartmentId")
	public DbResponse getUseListByDepartmentId(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
		try {
			JSONArray departmentIdList = jsonObject.getJSONArray("departmentIdList");
			String nodeId = jsonObject.getString("nodeId");
			String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
			if (CollectionUtils.isEmpty(departmentIdList)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "部门id不能为空！");
			}
			if (StringUtils.isEmpty(tenantId)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "租户id不能为空！");
			}
			List<String> departmentIds = commonUtil.splitDeptCode(departmentIdList.toJSONString());
			Map<String, Object> stringObjectHashMap = new HashMap<>();
			stringObjectHashMap.put("departmentIds",departmentIds);
			stringObjectHashMap.put("tenantId",tenantId);
			List<EwsUser> allUserList = userService.getAllUserList(stringObjectHashMap);
//			匹配节点处理人
			if (!StringUtils.isEmpty(nodeId)) {
//				allUserList = matchingNodeHandler(nodeId, allUserList);
			}
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, ResponseConstant.SELECT_SUCCESS_MSG, allUserList);
		} catch (Exception e) {
			log.error("获取用户信息异常信息：" + e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "获取用户信息异常信息：" + e);
		}
	}
	/**
	 * @Author: Donny_dong
	 * @Description: 匹配节点处理人
	 * @Param:
	 * @return:
	 * @Date: 2020/10/29
	 */
	private List<EwsUser> matchingNodeHandler(String nodeId,List<EwsUser> ewsUserList){
		WorkOrderTypeNodeVo workOrderTypeNodeVo = workOrderTypeNodeDao.selectById(Long.valueOf(nodeId));
		JSONObject nodeVoContent = workOrderTypeNodeVo.getContent();
		JSONArray deptUserValue = nodeVoContent.getJSONArray("deptUserValue");
		List<String> userIdList;
		if (!CollectionUtils.isEmpty(deptUserValue)) {
			userIdList = JSON.parseArray(deptUserValue.toJSONString(), String.class);
		}else {
			userIdList = ewsUserList.stream().map(e -> e.getId()).collect(Collectors.toList());
		}
		List<EwsUser> collect = ewsUserList.stream().filter(u -> userIdList.contains(u.getId())).collect(Collectors.toList());
		return collect;
	}

	@ApiOperation(value = "根据工单类型节点id获取坐席列表", notes = "根据工单类型节点id获取坐席列表")
	@RequestMapping(method = RequestMethod.GET, value = "/getUserListByWorkOrderTypeNodeId")
	public DbResponse getUserListByWorkOrderTypeNodeId(
			@ApiParam(value = "工单类型类型节点id", required = true) @RequestParam("workOrderTypeNodeId") String workOrderTypeNodeId,
			HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		if (StringUtils.isEmpty(workOrderTypeNodeId)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "工单类型节点id不能为空！");
		}
		try {
			return userService.getUserListByWorkOrderTypeNodeId(workOrderTypeNodeId,tenantId);
		} catch (Exception e) {
			log.info("根据工单类型节点id获取坐席列表异常" + e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "根据工单类型节点id获取坐席列表异常：" + e);
		}
	}
}
